<?php
class Debug{
	/**
	 * Script startup time
	 * @var float
	 */
	static protected  $_scriptStartTime = false;
	/**
	 * Database profiler
	 * @var Zend_Db_Profiler
	 */
	static protected  $_dbProfiler = false;
	
	static protected $_timers = array();
	
	
	static public function setScriptStartTime($time){
		self::$_scriptStartTime = $time;
	}
	
	static public function setDbProfiler(Zend_Db_Profiler $profiler){
		self::$_dbProfiler = $profiler;
	}

	/**
	 * Get debug information
	 * @return string
	 */
	static public function getStats($showQueries = false , $showAutoloaded = false , $showInclided = false){
		
		 $str = '';
		 
		 if(self::$_scriptStartTime)
		  	$str.= '<b>Time:</b> ' . number_format((microtime(true) - self::$_scriptStartTime),5) . "sec.<br>\n";
		
		 $str.= 
         '<b>Memory:</b> ' . number_format((memory_get_usage()/(1024*1024)),3 ) . "mb<br>\n".
         '<b>Memory peak:</b> ' . number_format((memory_get_peak_usage()/(1024*1024)),3 ) . "mb<br>\n".
         '<b>Includes:</b> ' . sizeof(get_included_files()). "<br>\n".
         '<b>Autoloaded:</b> ' . sizeof(Autoloader::getLoadedClasses()). "<br>\n";
		 
		 if(self::$_dbProfiler)
		 {	
		 	$str.='<b>Queries:</b> ' . self::$_dbProfiler->getTotalNumQueries(). '<br>'.
   		 	'<b>Queries time:</b> ' . number_format(self::$_dbProfiler->getTotalElapsedSecs(),5). 'sec.<br>';
		 	if($showQueries){
		 		$profiles = self::$_dbProfiler->getQueryProfiles();
		 		if(!empty($profiles))
		 			foreach ($profiles as $queryProfile)
		 				$str.="\n<br> ".$queryProfile->getQuery();
		 	}
		 	$str.= "<br>\n";
		 }
		 if($showAutoloaded)
		 	$str.= "<b>Autoloaded:</b>\n<br> " . implode("\n\t <br>", Autoloader::getLoadedClasses()).'<br>';
		 if($showInclided)	
		 	$str.= "<b>Includes:</b>\n<br> " . implode("\n\t <br>", get_included_files());
		 
		 return '<div style="position:fixed;font-size:12px;left:10px;bottom:10px;overflow:auto;padding:5px;background-color:#ffffff;z-index:1000;border:1px solid #cccccc;">'
		 		.$str.
		 		'</div>';
	}
	/**
	 * Start timer
	 * @param string $name
	 */
	static public function startTimer($name){
	    self::$_timers[$name] = array('start'=>microtime(true) , 'stop'=>0);
	}
	/**
	 * Stop timer
	 * @param string $name
	 * @throws Exception
	 * @return float time elapsed 
	 */
	static public function stopTimer($name){
	    if(!array_key_exists($name, self::$_timers))
	        throw new Exception('Invalid timer');
	        
	    self::$_timers[$name]['stop'] = microtime(true);
	    return  self::$_timers[$name]['stop'] - self::$_timers[$name]['start'];
	}
	/**
	 * Show time
	 * @param string $timer
	 * @throws Exception
	 * @return float time elapsed 
	 */
	static public function showTimerTime($timer){
	       if(!array_key_exists($timer, self::$_timers))
	        throw new Exception('Invalid timer');
	        
	       if(!self::$_timers[$timer]['stop'])
	           return self::stopTimer($timer);
	       
	    self::$_timers[$timer]['stop'] = microtime(true);
	    return  self::$_timers[$timer]['stop'] - self::$_timers[$timer]['start'];              
	}
	
}